package com.common方法;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.List;

/**
 * 收集常用的公共方法
 *
 * @author liyiruo
 * @Description
 * @Date 2021/11/5 下午4:41
 */
public class Utils {

    /**
     * 获取字符串中的最后一个 /a/b/c/d/e/     a/b/c/d/e    e    /e/
     *
     * @param s
     * @return
     */
    public static String getLastOne(String s) {
        String[] split = s.split("/");
        int len = split.length;
        String res = split[len - 1];
        return res;
    }

    /**
     * 从本地指定路径获取文件字节
     *
     * @param filePath
     * @return
     * @throws IOException
     */
    public byte[] getByteFromPath(String filePath) throws IOException {
        InputStream inputStream = new FileInputStream(filePath);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int length = 0;
        while (-1 != (length = inputStream.read(bytes))) {
            outputStream.write(bytes, 0, length);
        }
        return outputStream.toByteArray();
    }


    /**
     * 把字节数组保存为文件
     *
     * @param filename
     * @param data
     * @throws Exception
     */
    public static void saveFile(String filename, byte[] data) throws Exception {
        if (data != null) {
            String filepath = filename;
            java.io.File file = new java.io.File(filepath);
            if (file.exists()) {
                file.delete();
            }
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(data, 0, data.length);
            fos.flush();
            fos.close();
        }

    }

    /**
     * 从指定地址读取文件
     *
     * @param path
     * @return
     * @throws IOException
     */
    public static String getBytes(String path) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(path));
        StringBuffer buffer = new StringBuffer();
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        while (line != null) {
            sb.append(line);
            line = br.readLine();
        }
        return sb.toString();
    }

    /**
     *
     * @Title: pack
     * @Description: 将一组文件打成tar包
     * @param sources 要打包的原文件数组
     * @param target 打包后的文件
     * @return File 返回打包后的文件
     * @throws
     */
    public static File pack(List<File> sources, File target) {
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(target);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        TarArchiveOutputStream os = new TarArchiveOutputStream(out);
        for (File file : sources) {
            try {
                System.out.println(file.getName());
                os.putArchiveEntry(new TarArchiveEntry(file));
                IOUtils.copy(new FileInputStream(file), os);
                os.closeArchiveEntry();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (os != null) {
            try {
                os.flush();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return target;
    }
}
